package com.jinkaimen.goodcash.ui.weiget.dialog;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.ui.weiget.utils.DensityUtil;


/**
 * 自定义宽度dialog
 */

public class CustomWidthAlertDialog extends AlertDialog {
    private int width = 300;
    private Context context;
    private OnBtnClickListener[] tvBtnClickListener;
    private ViewHolder viewHolder;
    private boolean isAutoDismiss = true;

    public CustomWidthAlertDialog(Context context) {
        super(context, R.style.DialogTheme);
        this.context = context;
        viewHolder = new ViewHolder(View.inflate(context, R.layout.layout_dialog, null));
        setView(viewHolder.rootView);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    @Override
    public void setTitle(CharSequence title) {
        if (!TextUtils.isEmpty(title)) {
            viewHolder.tv_title.setText(title);
            viewHolder.tv_title.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_title.setVisibility(View.GONE);
        }
    }

    @Override
    public void setMessage(CharSequence message) {
        if (!TextUtils.isEmpty(message)) {
            viewHolder.tv_content.setText(message);
            viewHolder.tv_content.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_content.setVisibility(View.GONE);
        }
    }

    @Override
    public void setContentView(View view) {
        if (view != null) {
            viewHolder.tv_content.setVisibility(View.GONE);
            viewHolder.rl_content.addView(view);
        }
    }

    /**
     * 设置item点击监听
     *
     * @param tvBtnClickListener
     */
    public void setTvBtnClickListener(OnBtnClickListener[] tvBtnClickListener) {
        this.tvBtnClickListener = tvBtnClickListener;
        viewHolder.divier.setVisibility(tvBtnClickListener.length == 0 ? View.GONE : View.VISIBLE);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1, ViewGroup.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < tvBtnClickListener.length; i++) {
            if (i > 0) {
                View divider = View.inflate(context, R.layout.layout_divider, null);
                divider.setLayoutParams(params);
                viewHolder.ll_btn_content.addView(divider);
            }
            final OnBtnClickListener clickListener = tvBtnClickListener[i];
            if (clickListener == null)
                continue;
            final TextView dialogTvBtn = CustomButtons.getDialogTvBtn(context, clickListener.getTvBtnText());
            dialogTvBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isAutoDismiss && isShowing()) {
                        dismiss();
                    }
                    clickListener.onClick(CustomWidthAlertDialog.this, dialogTvBtn);
                }
            });
            if (tvBtnClickListener.length == 1) {
                dialogTvBtn.setBackgroundResource(R.drawable.shape_dialog_btn_right_left);
            } else {
                if (i == 0) {
                    dialogTvBtn.setBackgroundResource(R.drawable.shape_dialog_btn_left);
                } else if (i == tvBtnClickListener.length - 1) {
                    dialogTvBtn.setBackgroundResource(R.drawable.shape_dialog_btn_right);
                } else {
                    dialogTvBtn.setBackgroundResource(R.drawable.selector_dialog_item_bg_white_gray);
                }
            }
            viewHolder.ll_btn_content.addView(dialogTvBtn);
        }
    }


    /**
     * 设置dialog宽度
     *
     * @param width
     */
    public void setWidth(int width) {
        this.width = width;
    }

    @Override
    public void show() {
        if (!TextUtils.isEmpty(viewHolder.tv_title.getText().toString().trim()) && !TextUtils.isEmpty(viewHolder.tv_content.getText().toString().trim())) {
            viewHolder.linear_layout.setPadding(viewHolder.linear_layout.getPaddingLeft(), viewHolder.linear_layout.getPaddingTop(), viewHolder.linear_layout.getPaddingRight(), 0);
        }
        super.show();
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = (int) DensityUtil.applyDimension(context, TypedValue.COMPLEX_UNIT_DIP, width);
        getWindow().setAttributes(params);
    }

    private class ViewHolder {
        public View rootView;
        public TextView tv_title;
        public TextView tv_content;
        public View divier;
        public LinearLayout ll_btn_content;
        public LinearLayout linear_layout;
        public RelativeLayout rl_content;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            this.tv_content = (TextView) rootView.findViewById(R.id.tv_content);
            this.divier = rootView.findViewById(R.id.divider);
            this.ll_btn_content = (LinearLayout) rootView.findViewById(R.id.ll_btn_content);
            this.linear_layout = (LinearLayout) rootView.findViewById(R.id.linear_layout);
            this.rl_content = (RelativeLayout) rootView.findViewById(R.id.rl_content);
        }

    }

    public ViewHolder getViewHolder() {
        return viewHolder;
    }

    /**
     * 设置是否点击后自动消失
     *
     * @return
     */
    public boolean isAutoDismiss() {
        return isAutoDismiss;
    }

    public CustomWidthAlertDialog setAutoDismiss(boolean autoDismiss) {
        isAutoDismiss = autoDismiss;
        return this;
    }

    public static abstract class OnBtnClickListener {
        private String tvBtnText;

        public OnBtnClickListener(String tvBtnText) {
            this.tvBtnText = tvBtnText;
        }

        public String getTvBtnText() {
            return tvBtnText;
        }

        public abstract void onClick(CustomWidthAlertDialog alertDialog, View view);
    }
}
